package com.liuguangzhou.controller;

import com.alibaba.fastjson.JSONObject;
import com.liuguangzhou.CheckToken;
import com.liuguangzhou.LoginToken;
import com.liuguangzhou.model.User;
import com.liuguangzhou.utils.JwtUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description: TODO
 * @Author liuguangzhou
 * @Date 2020/3/29
 * @Version V1.0
 **/
@RestController
public class UserController {
//    @Autowired
//    private UserService userService;


    @PostMapping("/login")
    @LoginToken
    public Object login(User user) {

        JSONObject jsonObject = new JSONObject();
//        User userForBase = userService.findByUsername(user);
        User userForBase = new User(1, "Samule", "123456");
        if (userForBase == null) {
            jsonObject.put("message", "登录失败,用户不存在");
            return jsonObject;
        } else {
            if (!userForBase.getPassword().equals(user.getPassword())) {
                jsonObject.put("message", "登录失败,密码错误");
                return jsonObject;
            } else {
                String token = JwtUtil.createjwt(6000, userForBase);
                jsonObject.put("token", token);
                jsonObject.put("user", userForBase);
                return jsonObject;
            }
        }
    }

    /**
    * 功能描述:
    * 〈查看个人信息〉
    * @Param:
    * @Return:
    * @Author: liuguangzhou
    * @Date: 2020/3/29 19:27
    */
    @CheckToken
    @GetMapping("/getMessage")
    public String getMessage() {
        return "你已通过验证";
    }

}
